package com.solution.admin.order.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.solution.admin.order.constants.OrderConstants;
import com.solution.admin.order.dao.OrderDao;
import com.solution.admin.order.dao.OrderDeliveredBillDao;
import com.solution.admin.order.dao.OrderDetailDao;
import com.solution.admin.order.dao.OrderLogDao;
import com.solution.admin.order.entity.OrderDeliveredBillEntity;
import com.solution.admin.order.entity.OrderLogEntity;
import com.solution.admin.order.service.OrderDeliveredBillService;
import com.solution.common.constants.PublicStatus;
import com.solution.common.dao.BaseDao;
import com.solution.common.exception.BizException;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.service.impl.BaseServiceImpl;

/**
 * 发货单service实现层
 * @author lilp
 * @date 2017-09-18 22:56:20
 */
@Service
public class OrderDeliveredBillServiceImpl extends BaseServiceImpl<OrderDeliveredBillEntity> implements OrderDeliveredBillService {

	/**
	 * 发货单接口
	 */
	@Autowired
	private OrderDeliveredBillDao orderDeliveredBillDao;
	
	@Autowired
	private OrderDao orderDao;
	
	@Autowired
	private OrderDetailDao orderDetailDao;
	
	@Autowired
	private OrderLogDao orderLogDao;

	protected BaseDao<OrderDeliveredBillEntity> getDao() {
		return this.orderDeliveredBillDao;
	}

	/**
	 * 保存发货单
	 * @param orderDeliveredBillEntity 发货单实体
	 * @return Long 添加成功发货单的ID
	 * @author lilp
	 * @date 2017-09-18 22:56:20
	 */
	public long saveOrderDeliveredBill(OrderDeliveredBillEntity orderDeliveredBillEntity) {
		return orderDeliveredBillDao.saveOrderDeliveredBill(orderDeliveredBillEntity);
	}

	/**
	 * 分页查询发货单
	 * @param pageParam 分页参数
	 * @param paramMap 查询参数
	 * @return PageBean 分页参数和分页数据
	 * @author lilp
	 * @date 2017-09-18 22:56:20
	 */
	@Override
	public PageBean listOrderDeliveredBillPage(PageParam pageParam, Map<String, Object> paramMap) {
		return orderDeliveredBillDao.listOrderDeliveredBillPage(pageParam, paramMap);
	}
	
	/**
	 * 根据ID查询用户信息
	 * @param id 用户id
	 * @return orderDeliveredBillEntity 用户对象
	 * @author lilp
	 * @date 2017-09-18 22:56:20
	 */
	public OrderDeliveredBillEntity getOrderDeliveredBillById(Long id) {
		return orderDeliveredBillDao.getById(id.longValue());
	}

	/**
	 * 更新用户
	 * @param orderDeliveredBillEntity 用户实体
	 * @return Long 更新是否成功，>1代表更新成功
	 * @author lilp
	 * @date 2017-09-18 22:56:20
	 */
	@Override
	public int updateOrderDeliveredBill(OrderDeliveredBillEntity orderDeliveredBillEntity) {
		return orderDeliveredBillDao.updateOrderDeliveredBill(orderDeliveredBillEntity);
	}
	
	
	/**
	 * 根据条件查询发货单
	 * @param param
	 * @return 数据列表
	 * @author lilp
	 * @date 2017-09-18 22:56:20
	 */
	@Override
	public List<OrderDeliveredBillEntity> listOrderDeliveredBill( Map<String, Object> param){
		return  orderDeliveredBillDao.listOrderDeliveredBill(param);
	}
	
	/**
	 * 删除发货单
	 * @param 
	 * @return int 返回删除记录条数
	 * @author lilp
	 * @date 2017-09-18 22:56:20
	 */
	public int deleteOrderDeliveredBillById(long id){
		return  orderDeliveredBillDao.deleteById(id);
	}
	
	/**
	 * 查询已发货的商品清单
	 * @param paramMap
	 * @return
	 * @author lilp
	 * @date 2017-09-15 18:31:33
	 */
	public List<OrderDeliveredBillEntity> listOrderDetailDelivered( Map<String, Object> paramMap){
		return orderDeliveredBillDao.listOrderDetailDelivered(paramMap);
	}
	
	/**
	 * 全部发货
	 * @param param
	 * @return
	 */
	@Transactional(rollbackFor=Exception.class)
	public int sendFullOrder(Map<String, Object> param){
		int result = 0;
		try {
			// 插入发货单 
			OrderDeliveredBillEntity odbe = new OrderDeliveredBillEntity();
			odbe.setOrderCode((String)param.get("orderCode"));
			odbe.setDeliveryCode((String)param.get("deliveryCode"));
			odbe.setDeliveryCompanyCode((String)param.get("deliveryCompanyCode"));
			odbe.setIntro((String)param.get("intro"));
			odbe.setCreater((String)param.get("crater"));
			odbe.setSignStatus(OrderConstants.ORDER_SIGN_STATUS.NOT_SIGN.getCode());
			odbe.setDeliveryTime(new Date());
			odbe.setIsDelete(PublicStatus.IS_DELETE.NORMAL.getId());
			long id = orderDeliveredBillDao.insert(odbe);
			// 插入发货单  id= 发货单id
			param.put("deliveryBillId", id);
			result = orderDeliveredBillDao.insertDeliveredProduct(param);
			// 修改订单状态(待签收状态)
			param.put("orderStatus", OrderConstants.ORDER_STATUS.WAIT_TO_RECEIVE.getCode());
			param.put("sendTime", new Date());
			result += orderDao.updateOrderStatus(param);
			// 修改订单详情表的send_num 字段发货数量
			orderDetailDao.updateOrderDetailSendNumToBuyNum(param);
			//记录日志
			if (param.containsKey("orderLogEntity") && param.get("orderLogEntity") != null) {
				OrderLogEntity orderLog = (OrderLogEntity)param.get("orderLogEntity");
				orderLogDao.saveOrderLog(orderLog);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BizException("全部发货失败");
		}
		return result;
	}
	
	/**
	 * 部分发货
	 * @param param
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	public int sendPartOrder(Map<String, Object> param) {
		// 插入发货单
		OrderDeliveredBillEntity odbe = new OrderDeliveredBillEntity();
		odbe.setOrderCode((String) param.get("orderCode"));
		odbe.setDeliveryCode((String) param.get("deliveryCode"));
		odbe.setDeliveryCompanyCode((String) param.get("deliveryCompanyCode"));
		odbe.setIntro((String) param.get("intro"));
		odbe.setCreater((String) param.get("crater"));
		odbe.setSignStatus(OrderConstants.ORDER_SIGN_STATUS.NOT_SIGN.getCode());
		odbe.setDeliveryTime(new Date());
		odbe.setIsDelete(PublicStatus.IS_DELETE.NORMAL.getId());
		long lid = orderDeliveredBillDao.insert(odbe);
		// 插入发货单详情
		String[] ids = ((String)param.get("ids")).split(",");
		String[] sendNum = ((String)param.get("sendNum")).split(",");
		String[] detailIds = ((String)param.get("detailIds")).split(",");
		int result = 0;
		if(ids !=null && ids.length > 0){
			for (int i = 0 ; i < ids.length; i++) {
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("id", Long.parseLong(ids[i]));
				param1.put("sendNum", sendNum[i]);
				param1.put("detailId", detailIds[i]);
				param1.put("orderCode", (String)param.get("orderCode"));
				param1.put("deliveryCode", (String) param.get("deliveryCode"));
				// 发货单ID
				param1.put("deliveryBillId", lid);	
				if(Integer.parseInt(sendNum[i]) > 0){
					result += orderDeliveredBillDao.insertPartDeliveredProduct(param1);
					// 更新订单详情商品已发货数量
					orderDetailDao.updateOrderDetailSendNum(param1);
				}
			}
		}
		// 只有全部发完化， 才修改状态库待签收状态 ,前台用户根据根据是否完全发化， 显示待签收
		int notDeliveredNum = orderDetailDao.selectNotDeliveredNum(param);
		if(notDeliveredNum  == 0){
			// 修改订单状态(待签收状态)
			param.put("orderStatus", OrderConstants.ORDER_STATUS.WAIT_TO_RECEIVE.getCode());
			param.put("sendTime", new Date());
			orderDao.updateOrderStatus(param);
		}
		//记录日志
		if (param.containsKey("orderLogEntity") && param.get("orderLogEntity") != null) {
			OrderLogEntity orderLog = (OrderLogEntity)param.get("orderLogEntity");
			if (notDeliveredNum  == 0) {
				orderLog.setLogContent("订单已发货");
			}
			orderLogDao.saveOrderLog(orderLog);
		}
		return result;
	}
}
